package com.situ.tos2024.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.math.BigDecimal;

/**
 * 功能：订单详情
 *
 * @author 千堆雪
 * @version 1.0.0
 * @since 2024/4/10
 * <p>
 * created by 千堆雪 on 2024/4/10 10:41, last modified by 千堆雪 on 2024/4/10 10:41
 */
@Getter
@Setter
@ToString
@JsonIgnoreProperties("handler")
public class OrderDetail extends AbstractEntity {
    /**
     * 订单编号
     */
    private Integer orderId;

    /**
     * 数量
     */
    private Integer amount;

    /**
     * 小计
     */
    private BigDecimal subtotal;

    /**
     * 备注
     */
    private String description;

    /**
     * 菜品sku
     */
    private DishSku dishSku;

    /**
     * 对DishSku进行动态代理
     */
    public DishSku getDishSku() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(DishSku.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                Object result = method.invoke(dishSku, args);
                if (result == null && method.getName().startsWith("get")) {
                    if (method.getDeclaringClass() == DishSpu.class) {
                        result = method.invoke(dishSku.getDishSpu(), args);
                    }
                }
                return result;
            }
        });
        return (DishSku) enhancer.create();
    }
}